package com.jiyun.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiyun.model.information.ZlInstitution;
import com.jiyun.model.information.ZlTeam;
import com.jiyun.model.management.Team;
import com.jiyun.oss.AliyunOSSUtils;
import com.jiyun.service.IZlTeamService;
import com.jiyun.vo.ZlInstitutionVo;
import com.jiyun.result.Result;
import com.jiyun.service.ZlInstitutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/institution/")
@Slf4j
@Api(tags = "机构管理API接口")
public class ZlInstitutionController {
    @Autowired
    private ZlInstitutionService zlInstitutionService;

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ZlTeamController zlTeamController;

    @Autowired
    private IZlTeamService teamService;


//机构管理

    ///分页查询 单表
    @ApiModelProperty("机构管理分页查询接口（根据名称和地址模糊查询）")
    @RequestMapping("findPage/{pn}/{ps}")
    public Result findPage(@PathVariable Integer pn, @PathVariable Integer ps, @RequestBody ZlInstitutionVo vo) {
        log.info("条件查询：" + vo);
        IPage<ZlInstitution> page = zlInstitutionService.findPage(pn, ps, vo);
//        log.info("分页查询结果：" + page);
        return Result.ok(page);
    }

    ///添加,修改
    @ApiModelProperty("添加或修改机构管理信息")
    @RequestMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ZlInstitution institution) {
        log.info("添加或修改对象：" + institution);
        if (institution.getId() == null) {
            //添加
            institution.setStatus("1");
            zlInstitutionService.save(institution);
        } else {
            //修改
            zlInstitutionService.updateById(institution);
        }
        return Result.ok();
    }

//    状态变更
    @ApiModelProperty("修改状态")
    @RequestMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable String status) {
//        log.info("修改状态：" + id + ":" + status);
        ZlInstitution byId = zlInstitutionService.getById(id);
        byId.setStatus(status);
        zlInstitutionService.updateById(byId);
        return Result.ok();
    }

    //删除
    @Transactional
    @ApiModelProperty("删除")
    @RequestMapping("delete/{id}")
    public Result delete(@PathVariable Long id) {
        log.info("删除机构id：" + id);
        ZlInstitution byId = zlInstitutionService.getById(id);
//        删除机构团队
        int jid = Math.toIntExact(id);
        List<ZlTeam> zlTeamList = teamService.findJidTeam(jid);
        if (zlTeamList==null){
            System.err.println("nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
        }
        List<Integer> ids = zlTeamList.stream()
                .map(ZlTeam::getId)
                .collect(Collectors.toList());
        System.err.println("倒闭机构的团队===="+zlTeamList);
        ids.stream().forEach(i -> zlTeamController.deleteTeam(i));
//        zlTeamController.deleteTeam(zlTeam.getId());
//        删除机构
        zlInstitutionService.removeById(id);
        String dd = byId.getName()+"倒闭了";
        rabbitTemplate.convertAndSend("pei.jy", dd);
        return Result.ok();
    }

    //图片
    @ApiModelProperty("图片上传到阿里云oss")
    @PostMapping("/uufile")
    public Result upfile(MultipartFile file) throws IOException {
        String s = aliyunOSSUtils.putObject(file.getInputStream(), file.getOriginalFilename());
        return Result.ok(s);
    }

    @ApiModelProperty("机构列表查询")
    @RequestMapping("findName")
    public Result findName() {
        return Result.ok(zlInstitutionService.list());
    }

    @ApiModelProperty("根据ID查询")
    @RequestMapping("get/{id}")
    public ZlInstitution getById(@PathVariable Integer id) {
        return zlInstitutionService.getById(id);
    }



}
